﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using G15_Agent.Models.Hotel;
using G15_Agent.Helpers.ServiceHelpers;
using System.Web.Mvc;

namespace G15_Agent.Helpers.ServiceHelpers.HotelServiceHelpers
{
    public class HotelService : Service
    {
        virtual public List<HangPhong> IGetClassOfRoomList()
        {
            return null;
        }

        virtual public List<LoaiPhong> IGetTypeOfRoomList()
        {
            return null;
        }

        virtual public List<Phong> IRetrieveRoomList(
            string classRoomID, 
            string typeRoomID, 
            DateTime arriveDate, 
            DateTime returnDate)
        {
            return null;
        }

        virtual public List<Phong> ISearchForRoom(
            string classRoomID,
            string typeRoomID,
            DateTime arriveDate,
            double minPrice,
            double maxPrice)
        {
            return null;
        }

        virtual public string IBookingForRoom(
            List<Phong> listOfRoom, 
            DateTime arriveDate,
            DateTime returnDate)
        {
            return null;
        }

        public List<SelectListItem> GetClassOfRoomList()
        {
            string key =
                "serviceID" + this.ServiceID +
                "GetClassOfRoomList";
            List<HangPhong> classOfRoomList = null;
            // Kiểm tra dữ liệu trong cache
            classOfRoomList = (List<HangPhong>)HttpRuntime.Cache[key];
            if (classOfRoomList == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    classOfRoomList = this.IGetClassOfRoomList();
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        classOfRoomList = this.IGetClassOfRoomList();
                }
                finally
                {
                    if (classOfRoomList != null)
                        HttpRuntime.Cache[key] = classOfRoomList;
                }
            }
            List<SelectListItem> result = null;

            if (classOfRoomList != null)
            {
                result = new List<SelectListItem>();
                result.Add(new SelectListItem
                {
                    Value = @"null",
                    Text = @"Bất kì"
                });
                foreach (var roomClass in classOfRoomList)
                {
                    result.Add(new SelectListItem
                    {
                        Text = roomClass.tenHangPhong,
                        Value = roomClass.maHangPhong
                    });
                }
            }

            return result;
        }

        public List<SelectListItem> GetTypeOfRoomList()
        {
            string key =
                "serviceID" + this.ServiceID +
                "GetTypeOfRoomList";
            List<LoaiPhong> typeOfRoomList = null;
            // Kiểm tra dữ liệu trong cache
            typeOfRoomList = (List<LoaiPhong>)HttpRuntime.Cache[key];
            if (typeOfRoomList == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    typeOfRoomList = this.IGetTypeOfRoomList();
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        typeOfRoomList = this.IGetTypeOfRoomList();
                }
                finally
                {
                    if (typeOfRoomList != null)
                        HttpRuntime.Cache[key] = typeOfRoomList;
                }
            }

            List<SelectListItem> result = null;

            if (typeOfRoomList != null)
            {
                result = new List<SelectListItem>();
                result.Add(new SelectListItem
                {
                    Value = @"null",
                    Text = @"Bất kì"
                });
                foreach (var item in typeOfRoomList)
                {
                    result.Add(new SelectListItem
                    {
                        Text = item.tenLoaiPhong,
                        Value = item.maLoaiPhong
                    });
                }
            }

            return result;
        }

        public List<Phong> RetrieveRoomList(
            string classRoomID,
            string typeRoomID,
            DateTime arriveDate,
            DateTime returnDate)
        {
            string key =
                "serviceID" + this.ServiceID +
                "classRoomID" + classRoomID +
                "typeRoomID" + typeRoomID +
                "arriveDate" + arriveDate.ToShortDateString() +
                "returnDate" + returnDate.ToShortDateString();

            List<Phong> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<Phong>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.IRetrieveRoomList(classRoomID, typeRoomID, arriveDate, returnDate);
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.IRetrieveRoomList(classRoomID, typeRoomID, arriveDate, returnDate);
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }

        public List<Phong> SearchForRoom(
            string classRoomID,
            string typeRoomID,
            DateTime arriveDate,
            double minPrice,
            double maxPrice)
        {
            string key =
                "serviceID" + this.ServiceID +
                "classRoomID" + classRoomID +
                "typeRoomID" + typeRoomID +
                "arriveDate" + arriveDate.ToShortDateString() +
                "minPrice" +  minPrice.ToString() +
                "maxPrice" + maxPrice.ToString();

            List<Phong> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<Phong>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.ISearchForRoom(classRoomID, typeRoomID, arriveDate, minPrice, maxPrice);
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.ISearchForRoom(classRoomID, typeRoomID, arriveDate, minPrice, maxPrice);
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }

        public string BookingForRoom(
            List<Phong> listOfRoom,
            DateTime arriveDate,
            DateTime returnDate)
        {
            string result = null;
            try
            {
                if (!isLogin)
                    this.Login();
                result = this.IBookingForRoom(listOfRoom, arriveDate, returnDate);
            }
            catch (Exception ex)
            {
                if (this.Login())
                    result = this.IBookingForRoom(listOfRoom, arriveDate, returnDate);
            }

            return result;
        }
    }
}